package com.common.entity;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;

@Data
public class Result implements Serializable {
    private Integer code;
    private Object data;
    private String message;


    public static Result success() {
        Result result = new Result();
        /*result.setCode(1);
        result.setData(data);
        result.setMessage("正常");*/
        result.setCode(CodeConstant.SUCCESS.getCode());
        result.setMessage(CodeConstant.SUCCESS.getMessage());
        return result;
    }

    public static Result success(Object data) {
        Result result = new Result();
        /*result.setCode(1);
        result.setData(data);
        result.setMessage("正常");*/
        result.setCode(CodeConstant.SUCCESS.getCode());
        result.setData(data);
        result.setMessage(CodeConstant.SUCCESS.getMessage());
        return result;
    }

    public static Result fail() {
        Result result = new Result();
        /*result.setCode(-1);
        result.setMessage("失败");*/
        result.setCode(CodeConstant.FAIL.getCode());
        result.setMessage(CodeConstant.FAIL.getMessage());
        return result;
    }

    public static Result fail(int code) {
        Result result = new Result();
        result.setCode(code);
        result.setMessage(CodeConstant.onCode(code).getMessage());
        return result;
    }


    public static Result fail(String message) {
        Result result = new Result();
        result.setCode(CodeConstant.FAIL.getCode());
        result.setMessage(message);
        return result;
    }

    public static Result custom(int code, String message) {
        Result serverResult = new Result();
        serverResult.setCode(code);
        serverResult.setMessage(message);
        return serverResult;
    }


    public static Result page(Page<?> data) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("item", data.getRecords());
        map.put("total", data.getTotal());
        Result result = new Result();
        result.setCode(CodeConstant.SUCCESS.getCode());
        result.setData(map);
        return result;
    }

}
